import 'package:flutter_module/base/http/request_client.dart';
import 'package:flutter_module/model/api_response.dart';
import 'package:flutter_module/model/article.dart';
import 'package:flutter_module/model/article_tags.dart';
import 'package:flutter_module/model/page_data.dart';

class SecondPageRepository {
  int page = 0;
  bool canLoadMore = true;

  Future<List<Article<ArticleTags>>?> getArticleList(
      Function(PageData<Article<ArticleTags>>?)? onResponse,
      Function(String?)? onFailed,
      ) async {
    PageData<Article<ArticleTags>>? pageData = await requestClient.get<
        PageData<Article<ArticleTags>>>("/article/list/$page/json",
        fromJsonT: (json) => PageData.fromJson(
            json,
                (json) =>
                Article.fromJson(json, (json) => ArticleTags.fromJson(json))),
        onResponse: (ApiResponse<PageData<Article<ArticleTags>>?> response) {
          onResponse?.call(response.data);
        },
        onError: (e) {
          onFailed?.call(e.message);
        });
    page++;
    if (page >= pageData!.pageCount) {
      canLoadMore = false;
    }
    return pageData.datas;
  }
}